from plugin.dk.Network import NetWork
from ..router import docker
from flask import jsonify, request
from plugin.utils.request import input

network = NetWork()


@docker.route('network/list')
def getNetworkList():
    result = network.getList()

    data = {}
    data['code'] = 200
    data['data'] = result
    return jsonify(data)


@docker.route('network/remove', methods=['DELETE'])
def removeNetwork():
    for value in input('Id'):
        network.remove(value)
    data = {}
    data['code'] = 200
    data['message'] = 'success'
    return jsonify(data), data['code']


@docker.route('network/create', methods=['POST', 'GET', 'PUT'])
def createNetwork():
    from docker.types import IPAMConfig, IPAMPool

    formData = input()
    name = formData.pop('name')
    formData['ipam'] = {}
    ipam_pool = IPAMPool(
        subnet=formData.pop('subnet'),
        gateway=formData.pop('gateway')
    )
    formData['ipam'] = IPAMConfig(
        pool_configs=[ipam_pool]
    )

    network.create(name, **formData)
    data = {}
    data['code'] = 200
    data['message'] = 'success'
    return jsonify(data), data['code']
